//
//  LXPaymentApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXPaymentApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
#import "LXApplyAnnexTableCell.h"
#import "LXChooseManagerViewController.h"
#import "FuZeRenModel.h"
#import "LXAnnexShowTableViewCell.h"
#import "LXChooseCarViewViewController.h"
#import "LXChooseCustomViewController.h"
#import "KeHuInfoModel.h"
#import "LXCooperationListViewController.h"
#import "LXCooperationModel.h"



@interface LXPaymentApplyViewController()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;
@property (nonatomic) NSMutableArray *annexAry;
@property (nonatomic) NSMutableArray *annexDataAry;
// 收付款类型
@property (nonatomic) NSInteger fundType;
// 客户类型 0 客户 1 平台
@property (nonatomic) NSInteger customerType;
// 0 未结清 1已结清
@property (nonatomic) NSInteger settleState;
// 客户id
@property (nonatomic) NSString *customerId;


@property (nonatomic) NSString *carId;

@property (nonatomic) FuZeRenModel *fuzeModel;
@property (nonatomic) NSString *manageId;

@end

@implementation LXPaymentApplyViewController
- (void)viewDidLoad {
    [super viewDidLoad];

        
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"新增收款"];
    }
    else{
        [self layoutCustomNav:self.view title:@"新增付款"];
    }
    
    NSDictionary *dic = @{
        @"url":@"upLoad_B",
    };
    self.annexAry = [NSMutableArray array];
    self.annexDataAry = [NSMutableArray array];
    [self.annexAry addObject:dic];
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXApplyAnnexTableCell class]
         forCellReuseIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
    [self.tableView registerClass:[LXAnnexShowTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXAnnexShowTableViewCell class])];

    
    
    
    self.view.backgroundColor = White_Color;
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    [self setBottomViewWithText:@"确认新增"];

    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    
    // 默认未结清
    self.settleState = 1;
    // 默认个人客户
    self.customerType = 0;
    [self.loanDic setValue:@"客户" forKey:@"customerType"];
    
    if (self.type == 1) {
        
        [self.loanDic setValue:@"已结清" forKey:@"settleState"];
        
        if (!ObjectIsEmpty(self.model)) {
            
            // 应收日期
            [self.loanDic setValue:self.model.exceptReceiveDate forKey:@"date"];
            // 应收金额
            [self.loanDic setValue:self.model.amount forKey:@"amount"];
            // 实收日期
            [self.loanDic setValue:self.model.receiveDate forKey:@"actualDate"];
            // 实收金额
            [self.loanDic setValue:self.model.receiveAmount forKey:@"actualAmount"];
            // 付款人
            [self.loanDic setValue:self.model.receiveEmployeeName forKey:@"manageName"];
            self.manageId = self.model.receiveEmployeeId;
            
            // 应收类型
            NSString *fundType = [NSString getCollectionNameWithFundType:self.model.fundType];
            [self.loanDic setValue:fundType forKey:@"fundType"];
             
        }
        
        
       
    }
    else {
        
       
        [self.loanDic setValue:@"已付清" forKey:@"settleState"];
        /*
        // 应付日期
        [self.loanDic setValue:self.model.exceptPaidDate forKey:@"date"];
        // 应付金额
        [self.loanDic setValue:self.model.amount forKey:@"amount"];
        // 实付日期
        [self.loanDic setValue:self.model.paidDate forKey:@"actualDate"];
        // 实付金额
        [self.loanDic setValue:self.model.paidAmount forKey:@"actualAmount"];
        // 付款人
        [self.loanDic setValue:self.model.payEmployeeName forKey:@"manageName"];
        self.manageId = self.model.payEmployeeId;
        
        // 应付类型
        NSString *fundType = [NSString getPaidNameWithFundType:self.model.fundType];
        [self.loanDic setValue:fundType forKey:@"fundType"];
        */
    }
    
        

    self.data = @[
        
        @{
            @"title":@"*车牌号",
            @"key":@"carNumber",
            @"placeHolder":@"请选择车牌号",
            @"sel":@"clickCarNum",
            @"showAccess":@(YES),
            @"canEdit":@(NO),
            @"height":@(kComHeight)
        },
        @{
            @"title":@"*品牌型号",
            @"key":@"carBrand",
            @"placeHolder":@"自动识别车牌关联车型",
            @"height":@(kComHeight)
        },
        @{
            @"title":@"*客户类型",
            @"key":@"customerType",
            @"placeHolder":@"请选择客户",
            @"sel":@"clickCustomerTypeAction",
            @"showAccess":@(YES),
            @"height":@(kComHeight),
        },
        @{
            @"title":@"客户姓名",
            @"key":@"customerName",
            @"placeHolder":@"请选择客户",
            @"sel":@"clickCustomerAction",
            @"showAccess":@(YES),
            @"canEdit":@(NO),
            @"height":@(kComHeight),
        }.mutableCopy,
        @{
            @"title":self.type==2?@"*付款类型":@"*收款类型",
            @"key":@"fundType",
            @"placeHolder":self.type==2?@"请选择付款类型":@"请选择收款类型",
            @"sel":@"clickFundTypeAction",
            @"showAccess":@(YES),
            @"canEdit":@(NO),
            @"height":@(kComHeight),
        },
        @{
            @"title":self.type==2?@"*应付日期":@"*应收日期",
            @"key":@"yingDate",
            @"placeHolder":self.type==2?@"请选择应付日期":@"请选择应收日期",
            @"sel":@"clickYingDateAction",
            @"showAccess":@(YES),
            @"height":@(kComHeight),
        },
        @{
            @"title":self.type==2?@"*应付金额(元)":@"*应收金额(元)",
            @"key":@"yingAmount",
            @"placeHolder":self.type==2?@"请输入应付金额":@"请输入应收金额",
            @"canEdit":@(YES),
            @"inputType":@(NUM_DECIMAL),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"maxLength":@(7),
            @"height":@(kComHeight),
        },
        @{
            @"title":self.type==2?@"*实付日期":@"*实收日期",
            @"key":@"actualDate",
            @"placeHolder":self.type==2?@"请选择实付日期":@"请选择实收日期",
            @"sel":@"clickDateAction",
            @"showAccess":@(YES),
            @"canEdit":@(NO),
            @"height":@(kComHeight),
        },
        @{
            @"title":self.type==2?@"*实付金额(元)":@"*实收金额(元)",
            @"key":@"actualAmount",
            @"placeHolder":self.type==2?@"请输入实付金额":@"请输入实收金额",
            @"inputType":@(NUM_DECIMAL),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"maxLength":@(7),
            @"canEdit":@(YES),
            @"height":@(kComHeight),
        },
        @{
            @"title":@"*状态",
            @"key":@"settleState",
            @"placeHolder" : @"请选择状态",
            @"canEdit":@(NO),
//            @"showAccess":@(YES),
//            @"sel":@"clickSettleStateAction",
            @"height":@(kComHeight),
        },
        @{
            @"title":self.type==2?@"*付款人":@"*收款人",
            @"key":@"manageName",
            @"placeHolder" : self.type==2?@"请选择付款人":@"请选择收款人",
            @"canEdit":@(NO),
            @"showAccess":@(YES),
            @"sel":@"clickManageAction",
            @"height":@(kComHeight),
        },
        @{
            @"title":@"附加照片(最多4张)",
            @"key":@"annex",
            @"height":@(kComAnnexHeight)
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"height":@(150)
        }
    ];
    

    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        return [LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry] + 30;
    }
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];
     if ([dic[@"key"] isEqualToString:@"annex"]) {
        LXApplyAnnexTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLab.text = dic[@"title"];
        [cell reloadAnnexData:self.annexAry WithMaxNum:4];
        
        
        // 添加图片
        [cell setCallBackTakeAnnex:^(NSInteger num) {
            
            [[LXPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andMultipleImageBlock:^(id data) {
                
                [self uploadImage:data];
                
            } andSelectedNum:4 - self.annexDataAry.count];
            
        }];
        
        
        // 放大图片
        [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:self.annexDataAry];
            [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
            
        }];
        // 删除图片
        [cell setCallBackDeleteAnnex:^(NSInteger num) {
            
            [self.annexAry removeObjectAtIndex:num];
            [self.annexDataAry removeObjectAtIndex:num];

            [self.tableView reloadData];
        }];
        
        
        return cell;
    }
    
   else if([dic[@"key"]  isEqualToString:@"remark"]){
        // 备注
        LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.textView.placeholder = @"请输入备注...";
        cell.textView.maxLength = 200;
        cell.remarkTitle.text = @"备注";
        cell.textView.text = self.loanDic[@"remark"];
        [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
            [self.loanDic setValue:textStr forKey:@"remark"];
        }];
        return cell;
    }

    
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    
    cell.textfiledDidChangeBlock = ^(NSString * _Nonnull textStr) {
        if ([dic[@"key"] isEqualToString:@"actualAmount"]) {
            
            if ([textStr doubleValue] == [self.loanDic[@"yingAmount"] doubleValue]) {
                self.settleState = 1;
                if (self.type==1) {
                    self.loanDic[@"settleState"] = @"已结清";
                }
                else {
                    self.loanDic[@"settleState"] = @"已付清";
                }
               
            }
            else {
                self.settleState = 0;
                if (self.type==1) {
                    self.loanDic[@"settleState"] = @"未结清";
                }
                else {
                    self.loanDic[@"settleState"] = @"未付清";
                }
                
                
            }
            
            [self.tableView reloadData];
            
        }
    };
    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}
#pragma mark -- 选择应付应收日期
-(void)clickYingDateAction{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"yingDate"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        [self.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
    };
}


#pragma mark -- 选择实付实收日期
-(void)clickDateAction{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"actualDate"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        [self.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
        
    };
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight - 10) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = ColorWithF7F8FA;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}




#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    
    
    if (StringIsEmpty(self.loanDic[@"carNumber"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    
    
    if (self.type==1) {
        if (StringIsEmpty(self.loanDic[@"fundType"])) {
            showAlert(@"请选择收款类型");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"yingDate"])) {
            showAlert(@"请选择应收日期");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"yingAmount"])) {
            showAlert(@"请选择应收金额");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"actualDate"])) {
            showAlert(@"请选择实收日期");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"actualAmount"])) {
            showAlert(@"请选择实收金额");
            return;
        }
        if ([self.loanDic[@"actualAmount"] doubleValue] == 0) {
            showAlert(@"实收金额不能为0");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"manageName"])) {
            showAlert(@"请选择收款人");
            return;
        }
        
        
    }
    else {
        if (StringIsEmpty(self.loanDic[@"fundType"])) {
            showAlert(@"请选择付款类型");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"yingDate"])) {
            showAlert(@"请选择应付日期");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"yingAmount"])) {
            showAlert(@"请选择应付金额");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"actualDate"])) {
            showAlert(@"请选择实付日期");
            return;
        }
       
        if (StringIsEmpty(self.loanDic[@"actualAmount"])) {
            showAlert(@"请选择实付金额");
            return;
        }
        if ([self.loanDic[@"actualAmount"] doubleValue] == 0) {
            showAlert(@"实付金额不能为0");
            return;
        }
        if (StringIsEmpty(self.loanDic[@"manageName"])) {
            showAlert(@"请选择付款人");
            return;
        }
    }
   

   
    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        
        

        NSDictionary *dic;
        NSString *url;
        if (self.type==1) {
            
            dic = @{
                
                @"carId":self.carId, // 车辆id
                @"carNumber":self.loanDic[@"carNumber"],//车辆号码
                @"payerId":self.customerId,//付款客户id
                @"payerType":@(self.customerType),//付款客户类型
                @"payerName":self.loanDic[@"customerName"],//付款客户名称
                @"amount":self.loanDic[@"yingAmount"],// 应收金额
                @"receiveAmount":self.loanDic[@"actualAmount"],// 实收金额
                @"exceptReceiveDate":self.loanDic[@"yingDate"],// 应收日期
                @"receiveDate":self.loanDic[@"actualDate"],// 实收日期
                @"settleState":@(self.settleState), // 状态
                @"receiveEmployeeName":self.loanDic[@"manageName"], // 收款人
                @"receiveEmployeeId":self.manageId, //   收款人id
                @"fundType":@(self.fundType), //   收款类型
                @"pics":self.annexDataAry, //  图片
                @"remark":self.loanDic[@"remark"], //  备注
            };
            
            url = POST_AddTmoneyintoflow;
        }
        else {
            dic = @{
                
                @"carId":self.carId, // 车辆id
                @"carNumber":self.loanDic[@"carNumber"],//车辆号码
                @"payeeId":self.customerId,//收款客户id
                @"payeeType":@(self.customerType),//收款客户类型
                @"payeeName":self.loanDic[@"customerName"],//收款客户名称
                @"amount":self.loanDic[@"yingAmount"],// 应付金额
                @"paidAmount":self.loanDic[@"actualAmount"],// 实付金额
                @"exceptPaidDate":self.loanDic[@"yingDate"],// 应付日期
                @"paidDate":self.loanDic[@"actualDate"],// 实付日期
                @"settleState":@(self.settleState), // 状态
                @"payEmployeeId":self.manageId, // 付款人
                @"payEmployeeName":self.loanDic[@"manageName"], //   付款人`
                @"fundType":@(self.fundType), //   付款类型
                @"pics":self.annexDataAry, //  图片
                @"remark":self.loanDic[@"remark"], //  备注
            };
            
            url = POST_AddTmoneyoutflow;
        }
        
        
        
        [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (isSuccess) {
            
                showAlert(@"提交成功");
                
                kUserDefaultSetBool(YES, kRefreshStr);
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
 
}

//MARK: 上传附件
-(void)uploadImage:(NSArray *)ary{
    
    [self.annexAry removeLastObject];
    
    MBProgressHUD *hud = [MBProgressHUD showGif];
        
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
    dispatch_async(queue, ^{
    
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        [MSNetworkManager postRevertUrl:POST_UploadFile_URL params:@{@"type":@"1"} formData:@[obj] success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,resonseObj[@"url"]];

                [self.annexAry addObject:@{
                    @"url":imageUrl
                }];
                [self.annexDataAry addObject:resonseObj[@"url"]];
                
                if (idx == ary.count-1) {
                    [hud hide];
                    [self.annexAry addObject:@{
                        @"url":@"upLoad_B",
                    }];
                    [self.tableView reloadData];
                }
                dispatch_semaphore_signal(semaphore);
            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
            dispatch_semaphore_signal(semaphore);
            [hud hide];
        } withShowHud:NO];
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        
     }];
    });

}
//MARK: 选择结清状态
-(void)clickSettleStateAction{
    [self.view endEditing:YES];
    
    
    if (self.type==1) {
        [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"已结清" twoBtnTitle:@"未结清" cancelHandlerBlock:^{
        } oneHandlerBlock:^{
            self.settleState = 1;
            [self.loanDic setValue:@"已结清" forKey:@"settleState"];
            [self.tableView reloadData];
        } twoHandlerBlock:^{
            self.settleState = 0;
            [self.loanDic setValue:@"未结清" forKey:@"settleState"];
            [self.tableView reloadData];
        } onVC:self];
    }
    else {
        [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"已付清" twoBtnTitle:@"未付清" cancelHandlerBlock:^{
        } oneHandlerBlock:^{
            self.settleState = 1;
            [self.loanDic setValue:@"已付清" forKey:@"settleState"];
            [self.tableView reloadData];
        } twoHandlerBlock:^{
            self.settleState = 0;
            [self.loanDic setValue:@"未付清" forKey:@"settleState"];
            [self.tableView reloadData];
        } onVC:self];
    }
    
   
    
    
}

//MARK: 选择处理人
-(void)clickManageAction{
    
    [self.view endEditing:YES];
    
    
    LXChooseManagerViewController *vc = [[LXChooseManagerViewController alloc] init];
    WEAKSELF
    [vc setToSendValue:^(FuZeRenModel * _Nonnull model) {
        [weakSelf.loanDic setValue:model.realName forKey:@"manageName"];
        weakSelf.manageId = model.ID;
        weakSelf.fuzeModel = model;
        [weakSelf.tableView reloadData];
    }];
    vc.model = self.fuzeModel;
    [self pushToNextController:vc];
    
    
    
}
#pragma mark -- 选择车牌号
- (void)clickCarNum{
    
    LXChooseCarViewViewController *vc = [[LXChooseCarViewViewController alloc] init];
    vc.type = @"12";
    vc.comfrom = 1;
    [vc setToSendValue:^(CheLiangModel * _Nonnull model) {

    
        self.carId = model.ID;
        [self.loanDic setValue:model.carNum forKey:self.selDic[@"key"]];
        
        [self.loanDic setValue:[NSString stringWithFormat:@"%@ %@",model.carBrand,model.carMode] forKey:@"carBrand"];
        
        [self.tableView reloadData];
        
    }];
    [self pushToNextController:vc];
}
//MARK: 客户类型
-(void)clickCustomerTypeAction{
    
    [self.view endEditing:YES];
    
    [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"客户" twoBtnTitle:@"合作伙伴" cancelHandlerBlock:^{
    } oneHandlerBlock:^{
        
        
        if(self.customerType == 0)return;
        
        
        self.customerType = 0;
        [self.loanDic setValue:@"客户" forKey:self.selDic[@"key"]];
        

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"key MATCHES[cd] %@", @"customerName"];
        NSMutableDictionary *dic = [[self.data filteredArrayUsingPredicate:predicate] firstObject];
        
        [dic setValue:@"客户姓名" forKey:@"title"];
        [dic setValue:@"请选择客户" forKey:@"placeHolder"];
        
        [self.loanDic setValue:@"" forKey:dic[@"key"]];
        [self.tableView reloadData];
        
        
        
    } twoHandlerBlock:^{
        
        if(self.customerType == 1)return;
        
        self.customerType = 1;
        [self.loanDic setValue:@"合作伙伴" forKey:self.selDic[@"key"]];
        
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"key MATCHES[cd] %@", @"customerName"];
        NSMutableDictionary *dic = [[self.data filteredArrayUsingPredicate:predicate] firstObject];
        [dic setValue:@"合作伙伴" forKey:@"title"];
        [dic setValue:@"请选择合作伙伴" forKey:@"placeHolder"];
        
        [self.loanDic setValue:@"" forKey:dic[@"key"]];

        [self.tableView reloadData];
    } onVC:self];

    
}
//MARK: 选择客户
-(void)clickCustomerAction{
    
    [self.view endEditing:YES];
    // 个人
    if (self.customerType == 0) {
        
        LXChooseCustomViewController *vc = [[LXChooseCustomViewController alloc] init];
        [vc setToSendValue:^(KeHuInfoModel * _Nonnull model) {
            
            [self.loanDic setValue: model.customerName  forKey:@"customerName"];
            self.customerId = model.ID;
            [self.tableView reloadData];
        }];
        [self pushToNextController:vc];
        
    }
    // 企业
    else {
        
        LXCooperationListViewController *vc = [[LXCooperationListViewController alloc] init];
        [vc setClickVcCallBlock:^(LXCooperationModel * _Nonnull model) {
            
            [self.loanDic setValue:model.name forKey:@"customerName"];
            self.customerId = model.ID;
            [self.tableView reloadData];
        }];
        [self pushToNextController:vc];
        
        
        
    }
    
    
}
//MARK: 选择收付款类型
-(void)clickFundTypeAction{
    [self.view endEditing:YES];
   
    NSArray *ary;
    if(self.type == 1){
        ary = @[@"租金",@"租车押金",@"购车首付",@"购车月供",@"购车尾款",@"平台返佣",@"保险赔付",@"保险费",@"违约金",@"其他",@"车贷",@"介绍费",@"牌照费"];
    }
    else {
        ary = @[@"车贷",@"购车款(全款)",@"保险费用",@"年检费用",@"租车押金退还",@"签约佣金",@"车辆维修",@"保养费",@"中介费",@"牌照费",@"租金",@"违约金",@"保险理赔款"];
    }
    
    [NSString getCollectionNameWithFundType:1];
    SinglePickerView *singPickView = [[SinglePickerView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    for (int i = 0; i<ary.count; i++) {
        if ([self.loanDic[@"fundType"] isEqualToString:ary[i]]) {
            singPickView.selectInteger = i;
        }
    }
    [singPickView pickViewWithArray:ary andDescArray:nil andTitleStr:@"请选择类型"];
    [[UIApplication sharedApplication].delegate.window addSubview:singPickView];
    [singPickView showBottomView];
    singPickView.block = ^(NSString *singleTitle){
        [self.loanDic setObject:singleTitle forKey:self.selDic[@"key"]];
        
        if (self.type == 1) {
            self.fundType = [NSString getCollectionTypeNameWithName:singleTitle];
        }
        else {
            self.fundType = [NSString getPaidFundTypeWithName:singleTitle];

        }
    
        [self.tableView reloadData];
    
    
    };
    
}
@end
